// pages/news/news.js
const db = wx.cloud.database()
// console.log(db)
const audioContext = wx.createInnerAudioContext()
audioContext.autoplay = true
Page({

    /**
     * 页面的初始数据
     */
    data: {
        Hearing_id: '',
        books_list: '',
        play: true,
        duration: 0,
        currentTime: 0,
        sliderValue: 0,
        isSliderChaning: false
    },
    //获取听书数据
    getLemSpecial() {
        db.collection('LemSpecial').where({ 'mzyt.id': this.data.Hearing_id }).get().then(res => {
            var book = res.data[0].mzyt;
            for (var i = 0; i < book.length; i++) {  //遍历数组
                if (book[i]['id'] == this.data.Hearing_id)  //如果为数字，则返回该元素的值
                    res.data = book[i];
            }
            this.setData({
                books_list: res.data,
            }),
                audioContext.src = res.data.music,
                audioContext.onTimeUpdate(() => {
                    const currentTime = audioContext.currentTime * 1000
                    const duration = audioContext.duration * 1000
                    this.setData({ duration })
                    if (!this.data.isSliderChaning) {
                        const sliderValue = currentTime / this.data.duration * 100
                        this.setData({ sliderValue, currentTime })
                    }
                })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            Hearing_id: options.Hearing_id
        })
        console.log('bid:' + this.data.Hearing_id);
        this.getLemSpecial()
    },
    //播放
    play() {
        audioContext.play()
        this.setData({
            play: true,
        })
    },
    //暂停
    pause() {
        audioContext.pause()
        this.setData({
            play: false
        })
    },
    //记录时长
    handleSliderChange(e) {
        const value = e.detail.value
        const currentTime = this.data.duration * value / 100
        audioContext.pause()
        audioContext.seek(currentTime / 1000)
        this.setData({ sliderValue: value })
        setTimeout(() => {
            audioContext.play()
        }, 500);   // 给一个延迟，再播放
        this.setData({ sliderValue: value, isSliderChaning: false })
    },
    handleSliderChanging(e) {
        const value = e.detail.value
        const currentTime = this.data.duration * value / 100
        console.log(currentTime)
        this.setData({ isSliderChaning: true, currentTime, sliderValue: value })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})